<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
//use App\Http\Models\CarModel;
use App\Model\CarModelObjectInfo;
use Illuminate\Http\Request;
use App\Model\CarModelObject;
use App\Model\CarOil;

class ModelObjectController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    /**
     * 获取brands
     */
    public function getList( Request $request ){

        $res = [];
        $get = $request->all();
        $page = isset( $get['page'] )? intval($get['page']) : 1;
        $number = isset( $get['number'] )? intval($get['number']) : 20;
        $where = [];

        if( !empty( $get['brand_name'] ) ){
            $where[] =[ 'car_brand.name', 'like', '%' . $get['brand_name'] .'%'];
        }

        if( !empty( $get['factory_name'] ) ){
            $where[] =[ 'car_factory.name', 'like', '%' . $get['factory_name'] .'%'];
        }

        if( !empty( $get['model_name'] ) ){
            $where[] =[ 'car_model.name', 'like', '%' . $get['model_name'] .'%'];
        }

        if( !empty( $get['name'] ) ){
            $where[] =[ 'car_model_object.label', 'like', '%' . $get['name'] .'%'];
        }

        if( !empty( $get['year'] ) ){
            $where[] =[ 'car_model_object.year', 'like', '%' . $get['year'] .'%'];
        }

        if( !empty( $get['engine_displacement'] ) ){
            $where[] =[ 'car_model_object.engine_displacement', 'like', '%' . $get['engine_displacement'] .'%'];
        }

        if( !empty( $get['gearbox_lv'] ) ){
            $where[] =[ 'car_model_object.gearbox_lv', 'like', '%' . $get['gearbox_lv'] .'%'];
        }

        if( !empty( $get['gearbox_type'] ) ){
            $where[] =[ 'car_model_object.gearbox_type', 'like', '%' . $get['gearbox_type'] .'%'];
        }

        $list = CarModelObject::where( $where )
            ->leftJoin('car_model','car_model.id', '=', 'car_model_object.model_id')
            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->forPage($page, $number)
            ->select(
                'car_brand.id as brand_id'
                , 'car_brand.name as brand_name'
                , 'car_factory.id as factory_id'
                , 'car_factory.name as factory_name'
                , 'car_model.id as model_id'
                , 'car_model.name as model_name'
                , 'car_model_object.id'
                , 'car_model_object.label as name'
                , 'car_model_object.year'
                , 'car_model_object.engine_displacement'
                , 'car_model_object.gearbox_lv'
                , 'car_model_object.gearbox_type'
                )
        ->get();

        $count = CarModelObject::where( $where )
            ->leftJoin('car_model','car_model.id', '=', 'car_model_object.model_id')
            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->count();

        $res = [
            'status'=>0,
            'total'=> $count,
            'page_total'=> ceil( $count / $number),
            'list'=> $list
        ];
        echo json_encode( $res );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function single( $id ){

        $model = CarModelObject::where('car_model_object.id',$id)
            ->join('car_model_object_info','car_model_object_info.mi_key', '=', 'car_model_object.mi_key')
            ->leftJoin('car_model','car_model.id', '=', 'car_model_object.model_id')
            ->leftJoin('car_factory','car_factory.id', '=', 'car_model.factory_id')
            ->leftJoin('car_brand','car_brand.id', '=', 'car_factory.brand_id')
            ->select(
                'car_model_object_info.engine',
                'car_model_object_info.oil_id',
                'car_model_object_info.oil',
                'car_model_object_info.oil_change',
                'car_model_object_info.oil_regional',
                'car_model_object.mi_key',
                'car_model_object.year',
                'car_model_object.engine_displacement',
                'car_model_object.gearbox_lv',
                'car_model_object.gearbox_type',
                'car_model_object.id',
                'car_model_object.label as name',

                'car_model.id as model_id',
                'car_model.name as model_name',

                'car_factory.id as factory_id',
                'car_factory.name as factory_name',

                'car_brand.id as brand_id',
                'car_brand.name as brand_name'
            )
            ->first();
        //todo 拿到oil_id
        if( !empty( $model ) && empty($model->oil_id) ){
            $new_oil = CarOil::getOilByRule( $model->mi_key, $model->oil );
            $model->oil_id = !empty( $new_oil ) ? $new_oil['id'] : 0;
        }
        $single = !empty( $model ) ? $model : [];
        echo json_encode( $single );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function edit( Request $request, $model_object_id ){

        $post = $request->all();
        $params = $post;
        if( !empty( $model_object_id ) ){
            $res = CarModelObject::edit($model_object_id, $params);
        }else{
            $split = time() . rand(0,9);
            $mi_key = 'MI' . $split;
            $model_object = new CarModelObject();
            $model_object->model_id = $params['model_id'];
            $model_object->label = $params['name'];
            $model_object->year = $params['year'];
            $model_object->engine_displacement = $params['engine_displacement'];
            $model_object->gearbox_lv = $params['gearbox_lv'];
            $model_object->gearbox_type = $params['gearbox_type'];
            $model_object->mi_key = $mi_key;
            $res = $model_object->save();

            $model_object_info = new CarModelObjectInfo();
            $model_object_info->engine = $params['engine'];
            $model_object_info->year = $params['year'];
            $model_object_info->gearbox = $params['gearbox_type'];
            $model_object_info->gearbox_lv = $params['gearbox_lv'];
            $model_object_info->oil_id = $params['oil_id'];
//            $model_object_info->oil = $params['oil'];
            $model_object_info->oil_change = $params['oil_change'];
            $model_object_info->oil_regional = $params['oil_regional'];
            $model_object_info->mi_key = $mi_key;
            $res2 = $model_object_info->save();
            $res = $res && $res2;


        }

        echo json_encode( $res );
    }

}
